﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Core;
using ZHDJ.Data.DataCaching;
using ZHDJ.WebPage;

namespace ZHDJ.Web.App.Share
{
    public partial class FileUpload :DBPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            this.CKUpload();

        }

        private string AllExtensions
        {
            get
            {
                string str = System.Configuration.ConfigurationManager.AppSettings["SXT.Files.Extension"];
                if (string.IsNullOrEmpty(str))
                { return ".pdf;.doc;.docx;.xls;.xlsx;.ppt;.pptx;.rar;.zip;.jpg;.png;.jepg;.gif;.bmp;.txt;.swf;.avi;.rmvb;.rm;.asf;.divx;.mpg;.mpeg;.mpe;.wmv;.mp4;.mkv;.flv;.csf;.mp3;.wma;.wav"; }
                else
                { return str; }
            }
        }

      

        /// <summary>
        /// CKEditor文件上传
        /// </summary>
        private void CKUpload()
        {
            try
            {
                HttpPostedFile file = Request.Files["upload"];
                string fn = Request.QueryString["CKEditorFuncNum"] ?? "1";

                if (file != null)
                {
                    string fileName = file.FileName;

                    if (!this.AllExtensions.ToLower().Contains(Path.GetExtension(fileName).ToLower()))
                    {
                        this.ClientScript.RegisterClientScriptBlock(this.GetType(), "fileUpload", string.Format("window.parent.CKEDITOR.tools.callFunction('{0}', '', '不允许上传此类型文件，请重新选择文件上传！');", fn), true);
                        return;
                    }

                    var re = UpFileBusiness.Upload(file, CurrentUser.ID);
                    if (re.IsSuccess)
                    {
                        // 下面这句代码缺少的话，上传成功后上传队列的显示不会自动消失
                        this.ClientScript.RegisterClientScriptBlock(this.GetType(), "fileUpload", string.Format("window.parent.CKEDITOR.tools.callFunction('{0}', '{1}', '');", fn, UpFileBusiness.GetFileUrl(re.FileID)), true);
                    }
                    else
                    { this.ClientScript.RegisterClientScriptBlock(this.GetType(), "fileUpload", string.Format("window.parent.CKEDITOR.tools.callFunction('{0}', '', '文件上传失败！');", fn), true); }
                }
                else
                { this.ClientScript.RegisterClientScriptBlock(this.GetType(), "fileUpload", string.Format("window.parent.CKEDITOR.tools.callFunction('{0}', '', '文件上传失败！');", fn), true); }
            }
            catch (Exception ex)
            {
               
                this.ClientScript.RegisterClientScriptBlock(this.GetType(), "fileUpload", string.Format("window.parent.CKEDITOR.tools.callFunction('2', '', '文件服务器异常！');"), true);
            }
        }

       
    }
}